package com.syaccp.validation;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.syaccp.annotation.StringContains;

import cn.hutool.core.util.StrUtil;

/**
 * 校验一个字符串是否包含在指定字符数组内
 * @author Administrator
 *
 */
public class StringContainsValidator implements ConstraintValidator<StringContains, String>{
	
	
	 private Set<String> limitValues;

	    @Override
	    public void initialize (StringContains constraintAnnotation) {
	        limitValues = Arrays.stream(constraintAnnotation.limitValues()).collect(Collectors.toSet());
	    }

	    @Override
	    public boolean isValid(String value, ConstraintValidatorContext context) {
	        if (StrUtil.isBlank(value)) {
	            return true;
	        }
	        return limitValues.contains(value.trim());
	    }
	
	

}
